<?php
namespace Application\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Select;
use Zend\Db\ResultSet\ResultSet;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;

class ProductTable
{

    protected $tableGateway;

    public function __construct(TableGateway $tableGateway)
    {
        $this->tableGateway = $tableGateway;
    }

    public function getProductbyId($id)
    {
        $id = (int) $id;
        $rowset = $this->tableGateway->select(array(
            'id' => $id
        ));
        $row = $rowset->current();
        if (! $row) {
            throw new \Exception("Could not find row option $id");
        }
        return $row;
    }
    public function getProductbyArrayId($arrayid)
    {

        $rowset = $this->tableGateway->select(array(
            'id' => $arrayid
        ));

        if (empty($rowset)) {
            throw new \Exception("Could not find row option $id");
        }
        return $rowset;
    }

    public function fetchAll($paginated = false)
    {
        if ($paginated) {
            // create a new Select object for the table album
            $select = new Select('product');
            // create a new result set based on the Album entity
            $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype(new Product());
            // create a new pagination adapter object
            $paginatorAdapter = new DbSelect(
                // our configured select object
                $select,
                // the adapter to run it against
                $this->tableGateway->getAdapter(),
                // the result set to hydrate
                $resultSetPrototype);
            $paginator = new Paginator($paginatorAdapter);
            return $paginator;
        }
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }
    public function fetchTopProduct()
    {

       $sql = "select * from product where is_top=1 LIMIT 0, 7";
        /* @var $adapter \Zend\Db\Adapter\AdapterInterface */
        $adapter = $this->tableGateway->getAdapter();
        $statement =$adapter->query($sql);
        return $statement->execute();
    }
    public function fetchNewProduct()
    {

       $sql = "select * from product order by date_created LIMIT 0, 6";
        /* @var $adapter \Zend\Db\Adapter\AdapterInterface */
        $adapter = $this->tableGateway->getAdapter();
        $statement =$adapter->query($sql);
        return $statement->execute();
    }

    public function fetchByMenuID($menuID, $paginated = false)
    {
        if ($paginated) {
            // create a new Select object for the table album
            $select = new Select(array(
                'p' => 'product'
            ));
            $select = $select->join(array(
                'c' => 'category'
            ), "c.category_id=p.parent_id");

            // create a new result set based on the Album entity
            $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype(new Product());
            // create a new pagination adapter object
            $paginatorAdapter = new DbSelect(
                // our configured select object
                $select,
                // the adapter to run it against
                $this->tableGateway->getAdapter(),
                // the result set to hydrate
                $resultSetPrototype);
            $paginator = new Paginator($paginatorAdapter);
            return $paginator;
        }
        $resultSet = $this->tableGateway->select();
        return $resultSet;
    }

    public function fetchByGroupProductID($arrayGroupProductID, $paginated = false,$order_by,$order)
    {
        if ($paginated) {
            // create a new Select object for the table album
            $select = new Select('product');
            if(!empty($arrayGroupProductID)&&count($arrayGroupProductID)>0){
            $select = $select->where(array(
                "category_id" => $arrayGroupProductID
            ));
            }

           $select= $select->order(array($order_by=>$order));
            // create a new result set based on the Album entity
            $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype(new Product());
            // create a new pagination adapter object
            $paginatorAdapter = new DbSelect(
                // our configured select object
                $select,
                // the adapter to run it against
                $this->tableGateway->getAdapter(),
                // the result set to hydrate
                $resultSetPrototype);
            $paginator = new Paginator($paginatorAdapter);
            return $paginator;
        }
        $resultSet = $this->tableGateway->select(array(
                "category_id" => $arrayGroupProductID
            ));
        return $resultSet;
    }
    public function searchProduct($arrayGroupProductID, $paginated=false, $order_by, $order,
        $q,$typesearch,$searchname,$earchshortdesc,$searchdesc,
            $searchkeyword,$searchinsubcat,$productcode,$pricefrom,$priceto,$lbsfrom,$lbsto)
    {

//         var_dump($arrayGroupProductID);
        $where=array();
        if ($paginated) {
            // create a new Select object for the table album
            $select = new Select('product');
            if(!empty($arrayGroupProductID)&&count($arrayGroupProductID)>0){
                $where["category_id"] = $arrayGroupProductID;
            }
            if(!empty($q)){

                $select->where->like('name', "%".$q."%");
            }
            if(!empty($productcode)){

                $select->where->like('code', "%".$productcode."%");
            }
            if(!empty($pricefrom)&&empty($priceto)){
                $pricefrom=$priceto;
            }
            if(empty($pricefrom)&&!empty($priceto)){
               $priceto= $pricefrom;
            }


            if(!empty($pricefrom)&&!empty($priceto)){

                $select->where->between("price", $pricefrom, $priceto);
            }


            $select=$select->where($where);
//             var_dump($where);
//             var_dump($select);

           $select= $select->order(array($order_by=>$order));
            // create a new result set based on the Album entity
            $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype(new Product());
            // create a new pagination adapter object
            $paginatorAdapter = new DbSelect(
                // our configured select object
                $select,
                // the adapter to run it against
                $this->tableGateway->getAdapter(),
                // the result set to hydrate
                $resultSetPrototype);
            $paginator = new Paginator($paginatorAdapter);
            return $paginator;
        }
        $resultSet = $this->tableGateway->select($where);
        return $resultSet;
    }

    public function saveProduct(Product $product)
    {
        $data = array(
            'category_id' => $product->category_id,

            'code' => $product->code,
            'desc' => $product->desc,
            'short_desc' => $product->short_desc,
            'name' => $product->name,
            'price' => $product->price,
            'video' => $product->video,



        );
        if(!empty($product->product_file)){
            $data['product_file'] = $product->product_file;
            $data['product_file_type'] = $product->product_file_type;
            $data['product_file_size'] = $product->product_file_size;
            $data['product_file_name'] = $product->product_file_name;


        }
        if(!empty($product->banner)){
            $data['banner'] = $product->banner;
            $data['banner_type'] = $product->banner_type;
            $data['banner_size'] = $product->banner_size;
            $data['banner_name'] = $product->banner_name;

        }

        $id = (int) $product->id;

        if ($id == 0) {
            $this->tableGateway->insert($data);
        } else {
            if ($this->getProductbyId($id)) {
                $this->tableGateway->update($data, array(
                    'id' => $id
                ));
            } else {
                throw new \Exception('Form id does not exist');
            }
        }
        return $this->tableGateway->getLastInsertValue();
    }

    public function deleteProduct($id)
    {
        $this->tableGateway->delete(array(
            'id' => $id
        ));
    }
}